/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.sale;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.pos.I_SharedTicket;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.bean.sales.SharedTicket;

/**
 *
 * @author Administrator
 */
public class ShardTicketDAO extends BaseDAO<SharedTicket>{

    public ShardTicketDAO(I_Session s) {
        super(s);
    }

     @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                "SHAREDTICKETS",
                new Field[]{
                       new Field(I_SharedTicket.ID,Datas.STRING,Formats.STRING),
                       new Field(I_SharedTicket.NAME,Datas.STRING,Formats.STRING),
                       new Field(I_SharedTicket.CONTENT,Datas.BYTES,Formats.BYTEA),
                       new Field(I_SharedTicket.GROUPNAME,Datas.STRING,Formats.STRING),
                       new Field(I_SharedTicket.LOCATIONREF,Datas.STRING,Formats.STRING),
                       new Field(I_SharedTicket.LOCKED,Datas.BOOLEAN,Formats.BOOLEAN),
                       new Field(I_SharedTicket.LOCKUSERID,Datas.STRING,Formats.STRING)
                        },
                        new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, SharedTicket obj) throws BasicException {
         dp.setString(1,obj.getId());
         dp.setString(2,obj.getName());
         dp.setBytes(3, ImageUtils.writeSerializable(obj.getContent()));
         dp.setString(4,obj.getM_sGroupName());
         dp.setString(5,obj.getLocationId());
         dp.setBoolean(6, obj.isLocked());
         dp.setString(7,obj.getLockUserId());
    }

    @Override
    public Class getSuportClass() {
        return SharedTicket.class;
    }

    public SharedTicket readValues(DataRead dr,SharedTicket obj) throws BasicException {
        return null;
    }

	public void setSharedOrderLocked(final String id, boolean locked, final String userId) throws BasicException {
		Object[] values = new Object[] {id};
        Datas[] datas = new Datas[] {Datas.STRING};
        new PreparedSentence(s
                , "UPDATE SHAREDTICKETS SET LOCKED="+(locked==true?s.getDB().TRUE():s.getDB().FALSE())+" ,LOCKUSERID= ? "+"  WHERE ID = ?"
                ,SerializerWriteParams.INSTANCE ).exec(new DataParams() {
                    public void writeValues() throws BasicException {
                    	setString(1, userId);
                    	setString(2,id);
                    }});
		
	}

   public void unLockTicketByUser(String userId)throws BasicException{
	   new PreparedSentence(s
               , "UPDATE SHAREDTICKETS SET LOCKED="+s.getDB().FALSE()+"  WHERE LOCKED="+s.getDB().TRUE()+" AND LOCKUSERID = ?"
               ,SerializerWriteString.INSTANCE ).exec(userId);
   }
   
   public boolean isSharedTicketLocked(String id) throws BasicException{
	  return  (Boolean) findObjectPropVal(I_SharedTicket.LOCKED, id);
   }

   public void lockSharedTicket(String id, String userId) throws BasicException {
	  setSharedOrderLocked(id,true,userId);
   }

}
